package com.chance.cc.crawler.development.command.schedule.douyin;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.development.command.meta.keyword.CrawlerDomainKeywordCommand;
import com.chance.cc.crawler.meta.core.bean.crawler.CrawlerDomainKeys;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bx
 * @date 2020/12/15 0015 13:26
 */
@Data
@AllArgsConstructor
public class DYKeywordList {

    private String keyword;

    private String category;

//    private String businessTag;

    public static void main(String[] args) throws IOException {
        List<String> kwLines = IOUtils.readLines(new FileInputStream("D:\\chance\\gitspace\\chance-crawler-development\\crawler-scripts-command\\src\\main\\java\\com\\chance\\cc\\crawler\\development\\command\\schedule\\douyin\\dy_kw.txt"), Charset.forName("UTF-8"));
        common(kwLines);
//        apiAccount(kwLines);
    }

    private static void common(List<String> kwLines){
        for (String kwLine : kwLines) {
            if (kwLine.startsWith("#") || StringUtils.isBlank(kwLine)){
                continue;
            }
            String[] dyKeyword = kwLine.split("\t");

            CrawlerDomainKeywordCommand crawlerDomainKeywordCommand =
                    new CrawlerDomainKeywordCommand("192.168.1.215",9599);

            CrawlerDomainKeys crawlerDomainKeys = new CrawlerDomainKeys();
            crawlerDomainKeys.setDomain("douyin");
            crawlerDomainKeys.setKeyword(dyKeyword[0]);
            crawlerDomainKeys.setSite(dyKeyword[1]);
            crawlerDomainKeys.setStatus(0);
            HttpPage httpage = crawlerDomainKeywordCommand.addOrUpdateKeys(crawlerDomainKeys);
            Map map = JSON.parseObject(httpage.getRawText(), Map.class);
            if (!"0".equals(map.get("status").toString())){
                System.out.println(JSON.toJSONString(crawlerDomainKeys));
            }
        }
    }

    private static void apiAccount(List<String> kwLines){
        for (String kwLine : kwLines) {
            if (kwLine.startsWith("#")){
                continue;
            }
            String[] dyKeyword = kwLine.split("\t");

            CrawlerDomainKeywordCommand crawlerDomainKeywordCommand =
                    new CrawlerDomainKeywordCommand("192.168.1.215",9599);

            Map<String,String> keywordMap = new HashMap<>();
            keywordMap.put("unique_id",dyKeyword[0]);
            keywordMap.put("sec_uid",dyKeyword[1]);
            CrawlerDomainKeys crawlerDomainKeys = new CrawlerDomainKeys();
            crawlerDomainKeys.setDomain("douyin");
            crawlerDomainKeys.setKeyword(JSON.toJSONString(keywordMap));
            crawlerDomainKeys.setSite(dyKeyword[2]);
            crawlerDomainKeys.setStatus(0);
            crawlerDomainKeywordCommand.addOrUpdateKeys(crawlerDomainKeys);
//            crawlerDomainKeywordCommand.delKeys(crawlerDomainKeys);
            System.out.println(JSON.toJSONString(dyKeyword));
        }
    }
}
